import api from '../../utils/api.js'
import {
    showLoading,
    hideLoading
} from '../../utils/util.js'
import { HTTP } from '../../utils/http-pro.js'
import { config } from '../../utils/config'

const http = new HTTP()
const app = getApp();
const user_id = wx.getStorageSync("user_id")
const paramsData = {
    page: 1,
    size: 10,
}
const newParams0 = Object.assign({}, paramsData) //  下拉加载 page加 1
Page({
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        allOrder: [], //所有订单列表
        empty: false,
        imgUrl: config.imgUrl,
    },
    //联系商家
    phoneCall(e) {
        let phone = e.currentTarget.dataset.phone;
        let phone_other = e.currentTarget.dataset.phone_other;

        let phoneNumber = phone || phone_other;
        wx.makePhoneCall({
            phoneNumber: `${phoneNumber}`
        })
    },
    onLoad: function(options) {
        this._getRedundOrderById(paramsData).then((data) => {
            const allOrder = data.data
            if (allOrder.length == 0) {
                this.setData({
                    empty: true
                })
            }
            this.setData({
                allOrder
            })
        })

    },
    onShow: function() {},
    //获取全部订单，不包括待评***价，退款售后的订单

    _getRedundOrderById(params) {
        const user_id = wx.getStorageSync("user_id")
        return http.request({
            url: api.getRedundOrderById,
            data: {
                ...params,
                id: user_id
            }
        })
    },
    /*导航跳转*/
    gomyAllOrderList() {
        wx.redirectTo({
            url: '/pages/myAllOrderList/myAllOrderList',
        })
    },
    gomyEvaluationList() {
        wx.redirectTo({
            url: '/pages/myEvaluationList/myEvaluationList',
        })
    },
    //退款进度
    refundProgress(e) {
        const oid = e.currentTarget.dataset.order_id; //订单ID
        console.log(oid)
        var order = this.data.allOrder.filter((item) => {
            return item.id == oid
        })[0]
        order = JSON.stringify(order)
        wx.navigateTo({
            url: '/pages/refundProgress/refundProgress?order=' + order,
        })
    },
    //退款
    refundApply(e) {
        const oid = e.currentTarget.dataset.order_id; //订单ID
        var order = this.data.allOrder.filter((item) => {
            return item.id == oid
        })[0]
        order = JSON.stringify(order)
        wx.navigateTo({
            url: '/pages/refundApply/refundApply?order=' + order,
        })
    },
    //订单详情
    orderDetail(e) {
        const order_id = e.currentTarget.dataset.order_id;
        wx.navigateTo({
            url: `/pages/orderDetail/orderDetail?order_id=${order_id}`,
        })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

        let that = this;
        wx.showLoading({
                title: '加载中',
            })
            // 页数+1
        newParams0.page += 1;
        this._getRedundOrderById(newParams0).then((newData) => {
            console.log('newData', newData)
            newData = newData.data
            if (newData.length) {
                let allOrder_list = that.data.allOrder;
                for (let i = 0; i < newData.length; i++) {
                    allOrder_list.push(newData[i]);
                }
                // 设置数据
                that.setData({
                    allOrder: allOrder_list
                })
                wx.hideLoading();
            } else {
                console.log('已经到底了')
                    // wx.showToast({
                    //   title: '没有更多订单了',
                    //   icon: "none",
                    //   duration: 1500,
                    //   mask: true
                    // })
                this.setData({
                    loadingTip: true,

                })
                wx.hideLoading();
            }

        }).catch((error) => {
            console.log(error)
        })


    },
    onUnload: function() {
        newParams0.page = 1
    }
})